package lingou.homework.postbar.controller.proto.resp;

/**
 * Response
 */
public class Response {
    /**
     * 代码。-1：未知错误，0：成功
     */
    private long code;
    /**
     * 数据体
     */
    private Object data;
    /**
     * 错误信息
     */
    private String msg;

    public static final Response UNCOMPLETED = new Response()
            .setCode(1)
            .setMsg("有未提供的必要参数");
    public static final Response NO_PERMISSION = new Response()
            .setCode(1)
            .setMsg("无权限"); 
    public static final Response UNDONE = new Response()
            .setCode(999)
            .setMsg("未支持");
    public static final Response SUCCESS = ofSuccess(null);
    public static final Response UNKNOWN_ERROR = new Response()
            .setCode(-1)
            .setMsg("未知异常");
    public static Response ofUnknownError(){
        return UNKNOWN_ERROR;
    }
    public static Response ofUnknownError(String msg){
        return new Response()
                .setCode(-1)
                .setMsg(msg);
    }
    public static Response successExcept(boolean success, long code, String msg){
        return success ? SUCCESS : ofFailure(code, msg);
    }
    public static Response successExcept(boolean success, String msg){
        return success ? SUCCESS : ofFailure(msg);
    }
    public static Response ofSuccess(){
        return SUCCESS;
    }
    public static Response ofSuccess(Object data){
        return new Response()
                .setCode(0)
                .setData(data);
    }
    public static Response ofFailure(String msg){
        return ofFailure(1, msg);
    }
    public static Response ofFailure(long code, String msg){
        return new Response()
                .setCode(code)
                .setMsg(msg);
    }

    public long getCode() { return code; }
    public Response setCode(long value) {
        this.code = value;
        return this;
    }

    public Object getData() { return data; }
    public Response setData(Object value) {
        this.data = value;
        return this;
    }

    public String getMsg() { return msg; }
    public Response setMsg(String value) {
        this.msg = value;
        return this;
    }

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", data=" + data +
                ", msg='" + msg + '\'' +
                '}';
    }
}
